﻿//-----------------------------------------------------------------------
// <copyright file="CollisionUtility.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Common.Math
{
    using System;

    /// <summary>
    /// Utility class for handling common collision operations.
    /// </summary>
    public static class CollisionUtility
    {
        #region Methods - Public

        /// <summary>
        /// Tests to see if the circle contains the passed point.
        /// </summary>
        /// <param name="center">Circle center point.</param>
        /// <param name="radius">Circle radius.</param>
        /// <param name="pos">Point to test.</param>
        /// <returns>Returns true if the point is in the circle.</returns>
        public static bool PointInCircle(Vector2D center, float radius, Vector2D pos)
        {
            Vector2D toTestPt = pos - center;

            return toTestPt.Dot(toTestPt) <= MathUtility.Square(radius);
        }

        /// <summary>
        /// Test if a point is inside of an ellipse.
        /// </summary>
        /// <param name="center">Center of the ellipse.</param>
        /// <param name="radiusX">X radius of the ellipse.</param>
        /// <param name="radiusY">Y radius of the ellipse.</param>
        /// <param name="pos">Position to test.</param>
        /// <returns>Returns true if the point is in the ellipse.</returns>
        public static bool PointInEllipse(Vector2D center, float radiusX, float radiusY, Vector2D pos)
        {
            if (radiusX <= MathUtility.Epsilon)
            {
                throw new ArgumentException("Radius X must be greater than 0.0f");
            }

            if (radiusY <= MathUtility.Epsilon)
            {
                throw new ArgumentException("Radius Y must be greater than 0.0f");
            }

            Vector2D testPtLocal = pos - center;
            float componentX = MathUtility.Square(testPtLocal.x) / MathUtility.Square(radiusX);
            float componentY = MathUtility.Square(testPtLocal.y) / MathUtility.Square(radiusY);

            if ((componentX + componentY) > 1.0f)
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}
